package org.answer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.answer.dao.OrderDao;
import org.answer.dao.OrderSettingListDao;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.pojo.*;
import org.answer.service.OrderSettingListService;
import org.answer.service.OrderSettingService;
import org.answer.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private OrderDao orderDao;

    //添加套餐表数据回显
    public List<Setmeal> create() {

        List<Setmeal> create = orderSettingListDao.create();

        return create;
    }

    //分页查询
    public PageResult findPage (QueryPageBean queryPageBean) throws Exception{

        //获取本月第一天
        String date = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //当前页数量
        Integer pageSize = queryPageBean.getPageSize();
        //查询的条件
        String queryString = queryPageBean.getQueryString();
        //分页插件
        PageHelper.startPage(currentPage, pageSize);
        //查询
        Page<Map> page = orderSettingListDao.findPage(queryString,date);
        long total = page.getTotal();
        List<Map> result = page.getResult();
        for (Map map : result) {
            Date date1 = (Date) map.get("orderDate");
            System.out.println(date1);
            String s = DateUtils.parseDate2String(date1);
            System.out.println(s);
            map.put("orderDate",DateUtils.parseDate2String(date1));
        }
        return new PageResult(total,result);
    }

    //添加预约设置
    public void add(Member member, Integer[] setmealIds) {

        //获取输入进来的手机号
        String phoneNumber = member.getPhoneNumber();
        //根据手机号判断是否为会员
        Member member1 = orderSettingListDao.findMember(phoneNumber);
        if (member1 == null) {
            //不是会员，自动注册会员
            orderSettingListDao.add(member);
        }
        //反向获取新增会员的id
        Integer memberId = member.getId();
        Date regTime = member.getRegTime();
        //是会员查询是否本日已经预约了相同的项目，是否是重复预约
        //根据用户的手机号判断是否已经预约了相同的项目
        long phoneNumber1 = orderSettingListDao.findPhoneNumber(phoneNumber);
        if (phoneNumber1>0){
            //重复预约，预约失败
            return;
        }
        //遍历套餐项的id
        for (Integer setmealId : setmealIds) {
            Order order = new Order();
            order.setMemberId(memberId);
            order.setOrderDate(member.getRegTime());
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(setmealId);

            orderDao.add(order);
        }

    }

    //确认预约
    public void update(OrderSettingList orderSettingList) {

        try {
            String orderDate = DateUtils.parseDate2String(orderSettingList.getOrderDate()).substring(0,10);
            orderSettingList.setOrderDate(DateUtils.parseString2Date(orderDate));
            orderSettingListDao.update(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //取消预约
    public void delete(OrderSettingList orderSettingList) {
        try {
            String orderDate = DateUtils.parseDate2String(orderSettingList.getOrderDate()).substring(0,10);
            orderSettingList.setOrderDate(DateUtils.parseString2Date(orderDate));
            orderSettingListDao.delete(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
